//
//  UITableView+NoData.m
//  CarHousekeeper
//
//  Created by SDMac on 16/3/8.
//  Copyright © 2016年 shengdaqiche. All rights reserved.
//

#import "UITableView+NoData.h"

static NSInteger const kHZLTableViewNoData = 666;

@implementation UITableView (NoData)

- (void)setAlertView:(NSInteger)count title:(NSString*)title
{
    UILabel *alertLable = (UILabel*)[self viewWithTag:kHZLTableViewNoData];
    
    if (!alertLable)
    {
        alertLable = [[UILabel alloc]init];
        alertLable.tag = kHZLTableViewNoData;
        alertLable.font = [UIFont systemFontOfSize:20];
        alertLable.textColor = [UIColor blackColor];
        alertLable.textAlignment = NSTextAlignmentCenter;
        alertLable.bounds = CGRectMake(20, 0, self.frame.size.width - 40, 36);
        [self addSubview:alertLable];
    }
    alertLable.text = title;
    alertLable.center = CGPointMake(self.center.x, self.frame.size.height/2.0);
    
    if (alertLable && count > 0)
    {
        alertLable.hidden = YES;
    }
    else
    {
        alertLable.hidden = NO;
    }
}

- (void)setAlertView:(NSInteger)count title:(NSString*)title target:(id)target action:(SEL)action
{
    UIButton *alertButton = (UIButton*)[self viewWithTag:kHZLTableViewNoData];
    
    if (!alertButton)
    {
        alertButton = [UIButton buttonWithType:UIButtonTypeCustom];
        alertButton.tag = kHZLTableViewNoData;
        alertButton.bounds = CGRectMake(20, 0, self.frame.size.width - 40, 36);
        alertButton.center = CGPointMake(self.center.x, self.frame.size.height/2.0);
        alertButton.titleLabel.font = [UIFont systemFontOfSize:20];
        [alertButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [self addSubview:alertButton];
        
        if ([title isEqualToString:@"点击刷新"])
        {
            alertButton.enabled = YES;
        }
        else
        {
            alertButton.enabled = NO;
        }
    }
    
    if (alertButton && count > 0)
    {
        alertButton.hidden = YES;
    }
    else
    {
        alertButton.hidden = NO;
        
        [alertButton setTitle:title forState:UIControlStateNormal];
        [alertButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        
        if ([title isEqualToString:@"点击刷新"])
        {
            alertButton.enabled = YES;
        }
        else
        {
            alertButton.enabled = NO;
        }
    }
}

@end
